package com.liuyu.common.framework.rocketmq.properties;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * rocketmq 主题属性
 *
 * @author: liuyu
 * @date: 2022/4/24 8:29
 * @version: 1
 */
@Component
@ConfigurationProperties(prefix = "rocketmq.consumer")
@Data
@RefreshScope
public class RocketMqConsumeProperties {
    /**
     * 模式模式
     */
    private boolean strict = true;

    private Long suspendCurrentQueueTimeMillis = 3000L;

    private boolean skipErrors = false;
    /**
     * 严格模式
     */
    private List<ConsumeStrict> strictList = new ArrayList<>();

    private List<ConsumerGroup> consumerList = new ArrayList<>();

    /**
     * 根据主题和标签获取消费严格模式
     *
     * @param topic
     * @param tag
     * @return boolean
     * @throws
     * @author liuyu
     * @date 2022/6/8 10:26
     */
    public boolean getStrict(String topic, String tag) {
        for (ConsumeStrict consumeStrict : strictList) {
            //如果配置没有标签，则只判断标签
            if (StringUtils.isBlank(consumeStrict.getTag())) {
                if (topic.equals(consumeStrict.getTopic())) {
                    return consumeStrict.isStrict();
                }
            } else {
                //如果配置有标签，则同时判断主题和标签
                if (topic.equals(consumeStrict.getTopic()) && consumeStrict.getTag().equals(tag)) {
                    return consumeStrict.isStrict();
                }
            }
        }
        return strict;
    }

}
